<script>

import * as echarts from 'echarts';
import "echarts-wordcloud/dist/echarts-wordcloud";
import "echarts-wordcloud/dist/echarts-wordcloud.min";

export default {
  name: 'ChartFive',
  data() {
    return{

    }
  },
  mounted() {
    this.getData();
  },
  methods:{open1(msg) {
      this.$notify({
        title: msg,
        message: '这是一条成功的提示消息',
        type: 'success'
      });
    },
    open2(msg) {
      this.$notify({
        title: msg,
        message: '这是一条警告的提示消息',
        type: 'warning'
      });
    },
    open4(msg) {
      this.$notify.error({
        title: msg,
        message: '这是一条错误的提示消息'
      });
    },
    getData(){
      this.$axios.get('/api/data5', {

      }).then(res =>{

        if (res.data.code === 200) {
          let data = [];
          for (let i = 0; i < res.data.result.length; i++) {
            data.push(
                { value: res.data.result[i].count,
                  name: res.data.result[i].division
                }
            )
          }
          this.init(data);
        }

      }).catch(error =>{
        error = "系统出错了"
        this.open4(error)
      })
    },
    init(data) {

      var chartDom = document.getElementById("main-Chart-5");
      var myChart = echarts.init(chartDom);
      var option;
      option = {
        backgroundColor:"#fffdfd",
        title: {
          text: '分区占比',
        },
        tooltip: {
          trigger: "item",
          formatter: function (params) {
            var name = params.name;
            return (
                '<div style="display:flex; align-items:center;">' +
                '<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background-color:' +
                params.color +
                ';margin-right:5px;"></span>' +
                name +
                ": " +
                params.percent +
                    '%' +
                "</div>"
            );
          },
        },
        series: [
          {
            type: "pie",
            radius: ["0%", "100%"],
            avoidLabelOverlap: true,
            itemStyle: {
              borderRadius: 100,
              borderColor: "#fff",
              borderWidth: 2,
            },
            labelLine: {
              show: false,
            },
            data: data,
            label: {
              show: true,
              // position: "center",
              formatter: function (params) {
                var name = params.name;
                var percent = params.percent;
                return name + "\n" + percent +  '%';
              },
            },
          },
        ],
      };

      option && myChart.setOption(option);
    }
  }
}

</script>

<template>
  <div id="main-Chart-5"></div>
</template>

<style scoped>
#main-Chart-5 {
  height: 100%;
  width: 100%;
}
</style>